<?php
class AccountController extends LBController
{
    const ONLY_CHANGE_AVATR = 1;

    public function logoutAction()
    {/*{{{*/
        $nCrumb = $this->getParam('crumb');
        $oCrumb = $this->getCrumb();
        if ($nCrumb == $oCrumb)
        {
            $this->getSessSvc()->destroy('info');
            $this->redirect($this->getParam('desturl'));
        }
        //wrong crumb
        $this->redirect('/');
    }/*}}}*/

    public function updateProfileAction()
    {/*{{{*/
        $this->assign('title', gettext('update_profile'));

        $userInfo = $this->getUserInfo();
        $userId = $userInfo['userId'];
        $id = $userInfo['id'];

        $profile = new Profile();
        $profile->load($id);

        if ($this->_request->isPost())
        {
            $avatar = filter_var($this->getParam('avatar', ''), FILTER_VALIDATE_INT);

            $fileInfo = FileUploader::getFileInfo('headshot', $userId);

            if (self::ONLY_CHANGE_AVATR != $avatar)
            {
                $person = $this->getParam('person', array());

                $sex = filter_var($person['sex'], FILTER_VALIDATE_INT);
                $nickName = filter_var($person['nickName']);
                $realName = filter_var($person['realName']);
                $intro = filter_var($person['intro']);
                $birthday = filter_var($person['birthday']);

                $profile->setNickName($nickName);
                $profile->setSex($sex);
                $profile->setRealName($realName);
                $profile->setBirthday($birthday);
                $profile->setIntro($intro);
            }

            $profile->setHeadShot($fileInfo);
            $profile->save();

            if (self::ONLY_CHANGE_AVATR != $avatar)
            {
                return $this->renderAjax(gettext('update succ!'));
            }
        }
            
        $this->assign('nickName', $profile->getNickName());
        $this->assign('realName', $profile->getRealName());
        $this->assign('intro', $profile->getIntro());
        $this->assign('sex', $profile->getSex());
        $this->assign('birthday', $profile->getBirthday());
        $this->assign('headshotUrl', $profile->getHeadshotUrl());
        $this->assign('headshotId', $profile->getHeadshotId());
        $this->assign('skipSiderBar', true);
        $this->render();
    }/*}}}*/

    public function batchFetchAction()
    {
        //load as array, pass false to get the rowset, default=true
        $user = new User();
        $users = $user->findByIds(array(2,3), true);
        var_dump($users);
        exit;
        $this->render();
    }
}
